﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using ESCAPI_WRAPPER;
using System.Drawing;

namespace escapi_wrapper
{



    class Program
    {
        static void Main(string[] args)
        {
            int devices = Escapi.setupESCAPI();
            System.Console.WriteLine("devices {0}", devices);


            if (devices == 0)
            {
                System.Console.WriteLine("ESCAPI init failure or no devices found");
                return;
            }

            for (int i = 0; i < devices; i++)
            {
                string device_name = Escapi.getCaptureDeviceName((uint)i);
                System.Console.WriteLine("device number {0} is a {1}", i, device_name);
            }

            int pixels_wide = 24*2;
            int pixels_high = 18*2;
            SimpleCapParams capture = new SimpleCapParams(pixels_wide, pixels_high);


           
            /* Initialize capture - only one capture may be active per device,
             * but several devices may be captured at the same time. 
             *
             * 0 is the first device.
             */
	
            if (Escapi.initCapture(0, ref capture) == 0)
            {
                System.Console.WriteLine("Capture failed - device may already be in use.");
                return;
            }


	        /* now we have the data.. what shall we do with it? let's 
	         * render it in ASCII.. (using 3 top bits of green as the value)
	         */
            int screen_size = (pixels_wide + 1) * pixels_high;
            char[] screen = new char[screen_size]; // +1 for newlines
            char[] color_buf = new char[screen_size]; // +1 for newlines


            System.Console.ForegroundColor = ConsoleColor.White;
            System.Console.BackgroundColor = ConsoleColor.Black;


            unsafe
            {
                for (int frames = 0; frames < 100; frames++)
                {
                    /* request a capture */
                    Escapi.doCapture(0);

                    while (Escapi.isCaptureDone(0) == 0)
                    {
                        /* Wait until capture is done.
                         * Warning: if capture init failed, or if the capture
                         * simply fails (i.e, user unplugs the web camera), this
                         * will be an infinite loop.
                         */
                    }


                    


                    char[] light = " .,-o+O0@".ToCharArray();

                    System.Console.Clear();
                    

                    for (int i = 0; i < pixels_high; i++)
                    {
                        for (int j = 0; j < pixels_wide; j++)
                        {
                            int ofs = i * (pixels_wide) + j;
                            int ofs_pix_buf = i * (pixels_wide+1) + j;
                            int pixel = System.Runtime.InteropServices.Marshal.ReadInt32(capture.mTargetBuf, 4 * ofs);

                            /* now we have the data.. what shall we do with it? let's 
                            * render it in ASCII.. (using 3 top bits of green as the value)
                            */

                            int green = (pixel >> 13) & 7;
                            if (green >= 0 && green <= 7)
                            {
                                screen[ofs_pix_buf] = light[green];
                                color_buf[ofs_pix_buf] = (char)green;
                                //System.Console.ForegroundColor = (ConsoleColor)pixel2;
                                //System.Console.Write(light[pixel2]);

                            }
                            else
                            {
                                screen[ofs_pix_buf] = ' ';
                            }

                            

                        }
                        int ofs_pix_buf2 = i * (pixels_wide + 1) + pixels_wide;
                        screen[ofs_pix_buf2] = '\n';
                        //System.Console.WriteLine();
                    }

                    for (int i = 0; i < screen_size; i++)
                    {
                        System.Console.ForegroundColor = (ConsoleColor)color_buf[i];
                        System.Console.Write(screen[i]);
                    }

                    //System.Console.Write(screen);

                    System.Threading.Thread.Sleep(5);
                }


            }
        	
	        Escapi.deinitCapture(0);


            capture.freeMem();

            return;
            
        }
    }
}
